using System;
using System.Collections.Generic;
using TALoaders;
using System.Diagnostics;

namespace TALoaders.Tests
{
    using NUnit.Framework;

    [TestFixture]
    class TestBloombergLoader
    {
        private BloombergLoader bLoaderM = new BloombergLoader();

        [Test]
        [Category("Bloomberg")]
        public void getHistoricalData()
        {
            string ticker = "USO US Equity";
            // Test AdjClose
            SortedList<DateTime, double> close = bLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Close);
            Assert.AreEqual(37.24, close.Values[2], "Testing USO Equity Close");

            // Test Low
            SortedList<DateTime, double> lows = bLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Low);
            double totalLow = 0;
            foreach (double dailyLow in lows.Values)
                totalLow += dailyLow;
            double precision = 0.001;
            Assert.AreEqual(760.1099, totalLow, precision, "Testing USO Equity LOW");

            // Test Open
            SortedList<DateTime, double> open = bLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 19), new DateTime(2010, 2, 17), QuoteType.Open);
            Assert.AreEqual(open.Values[1], 38.23, "Testing USO OPEN");
        }

        [Test]
        [Category("Bloomberg")]
        public void getHistDataMissingDates()
        {
            string ticker = "SPGCEN Index";
            DateTime startDate = new DateTime(2008, 1, 1);
            DateTime endDate = new DateTime(2010, 9, 7);
            SortedList<DateTime, double> close = bLoaderM.getHistoricalPrices(ticker, startDate,endDate, QuoteType.Close);
            Assert.AreEqual(335.3429, close.Values[0], "Testing SPGCEN Index Close");
            Assert.AreEqual(676, close.Count, "Price array size differs");
        }

        [Test]
        [Category("Bloomberg")]
        public void getHistoricalDataMissingVolume()
        {
            string ticker = "OMX Index";
            DateTime start = new DateTime(1990, 1, 9);
            DateTime end = new DateTime(1990, 1, 20);
            SortedList<DateTime, Quote> quotes = bLoaderM.getHistoricalPrices(ticker, start, end);
            Quote quote = quotes[start];
            Assert.AreEqual(215.62, quote.getValue(QuoteType.Open), "OPEN");
            Assert.AreEqual(219.73, quote.getValue(QuoteType.High), "HIGH");
            Assert.AreEqual(215.62, quote.getValue(QuoteType.Low), "LOW");
            Assert.AreEqual(219.73, quote.getValue(QuoteType.Close), "CLOSE");
            Assert.AreEqual(0, quote.getValue(QuoteType.Volume), "VOLUME");
            Assert.AreEqual(219.73, quote.getValue(QuoteType.AdjClose), "AdjClose");
        }

        [Test]
        [Category("Bloomberg")]
        public void getHistoricalDataAllQuoteTypes()
        {
            string ticker = "USO US Equity";
            SortedList<DateTime, Quote> quotes = bLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 19), new DateTime(2010, 1, 23));
            Quote quote = quotes[new DateTime(2010, 1, 20)];
            Assert.AreEqual(38.23, quote.getValue(QuoteType.Open), "OPEN");
            Assert.AreEqual(38.35, quote.getValue(QuoteType.High), "HIGH");
            Assert.AreEqual(37.88, quote.getValue(QuoteType.Low), "LOW");
            Assert.AreEqual(37.89, quote.getValue(QuoteType.Close), "CLOSE");
            Assert.AreEqual(11444792, quote.getValue(QuoteType.Volume), "VOLUME");
            Assert.AreEqual(37.89, quote.getValue(QuoteType.AdjClose), "AdjClose");
            Assert.AreEqual(0.5 * (38.35 + 37.88), quote.getValue(QuoteType.AvgHL), "(H+L)/2");
            Assert.AreEqual((38.35 + 37.88 + 37.89) / 3.0, quote.getValue(QuoteType.AvgHLC), "(H+L+C)/3");
        }

        [Test]
        [Category("Bloomberg")]
        public void getLongHistoricalOMXData()
        {
            string ticker = "OMX Index";
            DateTime startDate = new DateTime(2009, 1, 1);
            DateTime endDate = new DateTime(2010, 6, 1);
            SortedList<DateTime, double> close = bLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.Close);
            Assert.AreEqual(354, close.Count, "Size of price array");
            Assert.AreEqual(693.78, close[new DateTime(2009, 1, 2)], "Close on 2009-01-02");
            Assert.AreEqual(607.45, close[new DateTime(2009, 3, 6)], "Close on 2009-03-06");
            Assert.AreEqual(981.25, close[new DateTime(2010, 6, 1)], "Close on 2009-06-01");
        }

        [Test]
        [Category("Bloomberg")]
        public void getLastPrice()
        {
            string ticker = "EDC US Equity";
            DateTime start = DateTime.Now;
            start = start.AddDays(-5);

            SortedList<DateTime, double> history = bLoaderM.getHistoricalPrices(ticker, start, DateTime.Now, QuoteType.Close);
            double lastHistory = history.Values[history.Count - 1];
            string timeStamp;
            double lastPrice = bLoaderM.getLastPrice(ticker, out timeStamp);
            Assert.AreEqual(lastHistory, lastPrice, 0.001, "Compare history and lastPrice"); // history gives the same result as last trade for todays date.
        }

        [Test]
        [Category("Bloomberg")]
        public void getLastPriceYahooBloomberg()
        {
            string bTicker = "UKX Index";
            string yTicker = "^FTSE";

            BaseLoader yLoader = new YahooDownloader();
            string timeStamp;
            double bLastPrice = bLoaderM.getLastPrice(bTicker, out timeStamp);
            double yLastPrice = yLoader.getLastPrice(yTicker, out timeStamp);
            Assert.AreEqual(yLastPrice, bLastPrice, 1, "Comparing DJI last price between Bloomberg and Yahoo");
        }

    }
}
